//
//  OCTool.m
//  EasyDana
//
//  Created by mac on 2024/10/19.
//

#import "OCTool.h"
#import <AdSupport/AdSupport.h>
#include <sys/param.h>
#include <sys/mount.h>
#import <mach/mach.h>
#import <mach/mach_host.h>
#import <sys/utsname.h>
#import <CoreLocation/CoreLocation.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <Network/Network.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <NetworkExtension/NetworkExtension.h>
#import <ifaddrs.h>
#import <arpa/inet.h>
@implementation OCTool
+(NSInteger)availableMemory{
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
    if (kernReturn != KERN_SUCCESS) {
        return 0;
    }
    long long availableMemorySize = ((vm_page_size * vmStats.free_count + vm_page_size * vmStats.inactive_count));
    return availableMemorySize;
}

+ (void)getNetworkTypeWithCompletion:(void (^)(NSString *networkType))completion {
    __block NSString *networkType = @"Unknown";

    // 创建网络监测对象
    nw_path_monitor_t monitor = nw_path_monitor_create();
    nw_path_monitor_set_queue(monitor, dispatch_get_main_queue());

    nw_path_monitor_set_update_handler(monitor, ^(nw_path_t path) {
        if (nw_path_uses_interface_type(path, nw_interface_type_wifi)) {
            networkType = @"WIFI";
        } else if (nw_path_uses_interface_type(path, nw_interface_type_cellular)) {
            // 使用 CoreTelephony 框架来进一步区分蜂窝网络类型
            CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
          NSString *currentRadioAccessTechnology = [[networkInfo.serviceCurrentRadioAccessTechnology allValues]firstObject];
            if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
                networkType = @"2G";
            } else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
                networkType = @"2G";
            } else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
                networkType = @"2G";
            } else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
                networkType = @"3G";
            } else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
                networkType = @"3G";
            } else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
                networkType = @"3G";
            } else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0] ||
                       [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA] ||
                       [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
                networkType = @"3G";
            } else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
                networkType = @"3G";
            } else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
                networkType = @"4G";
            } else if (@available(iOS 14.1, *)) {
                if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyNRNSA] ||
                    [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyNR]) {
                    networkType = @"5G";
                }
            } else {
                networkType = @"OTHER";
            }
        } else if (nw_path_uses_interface_type(path, nw_interface_type_wired)) {
            networkType = @"OTHER";
        } else if (nw_path_get_status(path) == nw_path_status_unsatisfied) {
            networkType = @"NONE";
        }

        nw_path_monitor_cancel(monitor);

        if (completion) {
            completion(networkType);
        }
    });

    nw_path_monitor_start(monitor);
}

+ (NSDictionary *)fetchSSIDInfo {
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    NSDictionary *info = nil;
    for (NSString *ifname in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);
        if (info && [info count]) {
            break;
        }
    }
    return info;
}
@end
